import { useTokenStore } from '@/stores/modules/authorization'
import instance from '../util/api'

//登录
export const doLogin = (data) => {
  const { username, password, code, uuid } = data
  return instance.post('/sso/doLogin', {
    username,
    password,
    code,
    uuid
  })
}
//登出
export const logout = async () => {
  instance.defaults.headers.common['Authorization'] = useTokenStore().getToken()
  const res = await instance.get('/sso/logout', {})
  return res.data
}

export const register = async (data) => {
  const { username, email, password, emailCode } = data
  const nickName = username
  const code = emailCode
  const res = await instance.post('/user/register', {
    nickName,
    email,
    password,
    code
  })
  return res
}
//发送邮件
export const sendMail = async (userEmail) => {
  const res = await instance.post(`/sso/send-mail`, { userEmail })
  return res.data
}

//TODO 上传头像自己写
export const uploadAvatar = () => {
  return '/user/uploadAvatar'
}
//修改用户密码
export const updateUserPwd = async (userId, oldPassword, newPassword) => {
  const res = await instance.post('/user/updateUserPwd', {
    userId,
    oldPassword,
    newPassword
  })
  return res
}

//修改用户名
export const updateUserInfo = async (userId, userName) => {
  const res = await instance.post('/user/updateUserInfo', {
    userId,
    userName
  })

  return res.data
}
//获取用户个人信息
export const getUserInfo = async (userId) => {
  instance.defaults.headers.common['Authorization'] = useTokenStore().getToken()
  const res = await instance.get(`/user/getUserInfo/${userId}`)
  return res.data
}
// 忘记密码
export const forgetPwd = async (data) => {
  const { email, emailCode, password } = data
  const code = emailCode
  const res = await instance.post('/user/forgetPassword', {
    email,
    code,
    password
  })
  return res.data
}
// ================================================

//检查用户账号
export const checkNickName = async (nickName) => {
  const res = await instance.post('/user/checkNickName', {
    nickName
  })
  return res.data
}

//获取图片验证码
export const getCaptcha = async () => {
  const res = await instance.get('/sso/getCaptcha')
  return res.data
}
//发送邮箱//检查邮箱是否被绑定
// export const checkMail = async (userEmail, emailCode) => {
//   const res = await instance.post('/sso/check-mail', {
//     userEmail,
//     emailCode
//   })
//   return res.data
// }

// const token = useTokenStore()
// // 登录
// const loginService = async (data) => {
//   // 登录接口
//   const res = await instance.post('/sso/doLogin', {
//     username: data.username,
//     password: data.password
//   })
//   token.setToken(res.data.token)
//   return res.data
// }
// //注册
// const registryService = async (data) => {
//   const res = await instance.post('/registry', {
//     username: data.username,
//     password: data.password
//   })
//   console.log(res)
// }

// //退出
// const loginoutService = async () => {
//   const Authorization = token.getToken()
//   instance.headers.Authorization = Authorization
//   const res = await instance.post('/loginout')
//   console.log(res)
//   token.removeToken()
// }

// export default { loginService, registryService, loginoutService }
